package com.srcbox.file.adapter

import android.app.Activity
import android.graphics.BitmapFactory
import android.os.Environment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.srcbox.file.R
import com.srcbox.file.util.EggUtil
import com.test.showdir.data.FileAdapterShare
import java.io.File
import java.io.FileFilter


class FilesList(
    private val context: Activity,
    private var filesL: Array<File?>
) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    init {
        val pathStr = "${context.filesDir.absolutePath}/···"
        File(pathStr).let {
            if (!it.exists()) {
                it.mkdirs()
            }
        }
        filesL = EggUtil.concat<File>(arrayOf(File(pathStr)), filesL)
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.files_list, parent, false)
        return MeHolder(view)
    }

    override fun getItemCount(): Int {
        return filesL.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

        val file = filesL[position]
        if (file!!.isDirectory) {
            if (holder is MeHolder) {
                holder.bind(file)
            }
        }
    }

    inner class MeHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bind(file: File?) {

            itemView.findViewById<TextView>(R.id.fName).text = file?.name
            itemView.findViewById<ImageView>(R.id.fIcon)
                .setImageBitmap(BitmapFactory.decodeStream(context.assets.open("img/dir.png")))
            itemView.setOnClickListener {
                if (file?.name == "···") {
                    if (FileAdapterShare.currentFilePath == Environment.getExternalStorageDirectory().absolutePath) {
                        context.finish()
                        return@setOnClickListener
                    } else {
                        val fileP = File(FileAdapterShare.currentFilePath).parentFile
                        val p = fileP?.listFiles(FileFilter {
                            it.isDirectory
                        })
                        p?.sort()
                        context.findViewById<RecyclerView>(R.id.filesListV).adapter =
                            FilesList(context, p!!)
                        FileAdapterShare.currentFilePath = fileP.absolutePath
                        context.findViewById<TextView>(R.id.title_text).text =
                            FileAdapterShare.currentFilePath
                        return@setOnClickListener
                    }
                }


                val filePath = "${FileAdapterShare.currentFilePath}/${file?.name}"
                val p = File(filePath).listFiles(FileFilter {
                    it.isDirectory
                })
                p?.sort()
                context.findViewById<RecyclerView>(R.id.filesListV).adapter =
                    FilesList(context, p!!)
                FileAdapterShare.currentFilePath = filePath
                context.findViewById<TextView>(R.id.title_text).text =
                    FileAdapterShare.currentFilePath
            }
        }
    }
}